<template>
	<view class="myExchange">
		<!-- <header-box :title="pageTitle"></header-box> -->
		<view class="myExchange-box">
			<view class="myExchange-main">
				<view class="myExchange-main-title"><text>兑换</text></view>
				<view class="myExchange-main-content">
					<input class="_content-input" type="text" v-model="exchange" placeholder="请输入兑换码" />
					<view class="_content-btn" @click="queryExchange(exchange)"><text>兑换</text></view>
				</view>
				<view class="myExchange-main-list">
					<view class="myExchange-main-title"><text>可兑换清单</text></view>
					<mescroll-uni ref="mescrollRef" :isBack="true" @init="mescrollInit" height="100%" :top="mescrollUni_Top" :bottom="mescrollUniBottom" :total="total" :down="downOption" @down="downCallback" :up="upOption" @up="upCallback" @emptyclick="emptyClick">
						<view class="myExchange-main-list-item" v-for="item,index in list" :key="index">
							<view class="_list-item-label"><text>{{item.exchangeCode}}</text></view>
							<view class="_list-item-btn" @click="queryExchange(item.exchangeCode)"><text>一键兑换</text></view>
						</view>
					</mescroll-uni>
				</view>
			</view>
		</view>
		
		<!-- 兑换弹窗 -->
		<u-popup :show="exchangeShow" :round="20" mode="center"  :overlayOpacity="0.9" @close="exchangePopupClose" @open="exchangePopupOpen" bgColor="transparent">
			<view class="exchangePopup" v-if="exchangeData">
				<view class="exchangeBox">
					<view class="exchangeBox-goods">
						<image class="exchangeBox-goods-img" :src="exchangeData.img" mode=""></image>
					</view>
					<view class="exchangeBox-title single-line"><text>{{exchangeData.name}}</text></view>
					<view class="exchangeBox-money"><text>¥{{exchangeData.price}}</text></view>
				</view>
				<view class="exchange-btn" @click="exchangeConfirm"><text>确定兑换</text></view>
			</view>
		</u-popup>
		
		<!-- 弹窗 -->
		<u-popup :show="successShow" :round="20" mode="center" :overlayOpacity="0.8" @close="successPopupClose" @open="successPopupOpen" bgColor="transparent">
			<view class="tipsPopup">
				<view class="tipsPopup-icon">
					<image class="tipsPopup-icon-img" src="../../static/img/success-icon.png" mode=""></image>
				</view>
				<view class="tipsPopup-text"><text>兑换成功</text></view>
				<view class="tipsPopup-confirm" @click="confirmOk"><text>知道啦</text></view>
				<image class="cancel-icon" @click="successShow = false" src="../../static/img/cancel-icon.png" mode=""></image>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";
	import MescrollMoreItemMixin from "@/components/mescroll-uni/mixins/mescroll-more-item.js";
	export default {
		mixins: [MescrollMixin,MescrollMoreItemMixin], // 注意此处还需使用MescrollMoreItemMixin (必须写在MescrollMixin后面)
		data() {
			return {
				pageTitle:'兑换',
				exchange:'',
				exchangeShow:false,
				successShow:false,
				total:0,
				list:[],
				params:{
					page: 1, //分页页码，数字类型
					pageSize: 8
				},
				downOption:{
					auto:false // 不自动加载 (mixin已处理第一个tab触发downCallback)
				},
				upOption:{
					auto:false, // 不自动加载
					page: {
						num: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
						size: 10 // 每页数据的数量
					},
					noMoreSize: 6, //如果列表已无数据,可设置列表的总数量要大于半页才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看; 默认5
					toTop:{
						offset:400,
					},
					empty:{
						tip: '', // 提示
						btnText: ''
					},
					elItem:{
						top:256,
						height:265,
						rowNum:2,
					}
				},
				mescrollUni_Top:0,
				mescrollUniBottom:0,
				exchangeData:null,
				queryExchangeFlag:true,
			};
		},
		onLoad() {
			
		},
		mounted() {
			this.mescroll.setPageNum(1)
			this.mescroll.resetUpScroll(true)
			this.mescroll.scrollTo(0,300)
		},
		methods:{
			/*下拉刷新的回调 */
			downCallback() {
				// 这里加载你想下拉刷新的数据, 比如刷新轮播数据
				// loadSwiper();
				// 下拉刷新的回调,默认重置上拉加载列表为第一页 (自动执行 page.num=1, 再触发upCallback方法 )
				this.mescroll.resetUpScroll()
			},
			/*上拉加载的回调: 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10 */
			upCallback(page) {
				// console.log(page.num)
				this.params.page = page.num
				this.getList()
			},
			//点击空布局按钮的回调
			emptyClick(){
				uni.showToast({
					title:'点击了按钮,具体逻辑自行实现'
				})
			},
			//获取订单列表
			getList(){
				this.mescroll.lockUpScroll(true)
				this.$api.getExchangeList(this.params).then(res => {
					console.log(res)
					let curLen = this.list.length
					let totalPage
					if(res.code == 200){
						if(this.params.page == 1) this.list = []
						this.list = this.list.concat(res.data.records)
						curLen = this.list.length
						totalPage = Math.ceil(res.data.total / this.params.pageSize)
						setTimeout(() => {
							this.mescroll.endByPage(curLen, totalPage);
						},200)
						this.total = res.data.total
					}
					// console.log(this.upOption.toTop)
					// this.mescroll.endSuccess(curLen)
					setTimeout(() => {
						this.mescroll.lockUpScroll(false)
					},800)
				},err => {
					setTimeout(() => {
						this.mescroll.lockUpScroll(false)
					},800)
					this.mescroll.endErr()
				})
			},
			queryExchange(exchange) {
				if (!this.queryExchangeFlag) return false
				if (!exchange) {
					uni.showToast({
						title:'请输入兑换码',
						icon:'none'
					})
					return false
				}
				this.queryExchangeFlag = false;
				this.$api.getExchangeQry({exchangeCode:exchange}).then(res => {
					// console.log(res)
					this.queryExchangeFlag = true;
					if (res.code == 200) {
						this.exchangeData = res.data;
						this.exchangeShow = true;
					}
				})
				// this.$api.getExchangeDetail().then(res => {
				// 	console.log(res)
				// })
			},
			exchangePopupOpen() {
				this.exchangeShow = true;
			},
			exchangePopupClose() {
				this.exchangeShow = false
				// console.log('close');
			},
			exchangeConfirm() {
				this.exchangeShow = false;
				if (!this.exchangeData.exchangeId) return false
				this.$api.getExchange({exchangeId:this.exchangeData.exchangeId}).then(res => {
					// console.log(res)
					if (res.code == 200) {
						this.exchange = '';
						this.successShow = true;
						this.mescroll.setPageNum(1)
						this.mescroll.resetUpScroll(true)
						this.mescroll.scrollTo(0,300)
					}
				})
			},
			successPopupOpen() {
				this.successShow = true;
			},
			successPopupClose() {
				this.successShow = false
				// console.log('close');
			},
			confirmOk() {
				this.successShow = false;
			},
		}
	}
</script>

<style lang="less">
	.myExchange{
		width: 100%;
		min-height: 100vh;
		background-color: #181A20;;
		font-family: PingFangSC-Medium, PingFang SC, '微软雅黑';
		font:caption;
		display: flow-root;
		.myExchange-box{
			padding: 0 48rpx;
			box-sizing: border-box;
			.myExchange-main{
				width: 100%;
				margin-top: 20rpx;
				.myExchange-main-title{
					font-size: 24rpx;
					font-weight: 400;
					color: #5E6272;
					line-height: 32rpx;
				}
				.myExchange-main-content{
					width: 100%;
					height: 96rpx;
					display: flex;
					justify-content: space-between;
					align-items: center;
					margin-top: 16rpx;
					._content-input{
						width: calc(100% - 162rpx);
						height: 100%;
						padding: 0 32rpx;
						box-sizing: border-box;
						font-size: 32rpx;
						font-weight: 600;
						color: #5E6272;
						line-height: 96rpx;
						background: #16181D;
						border-top-left-radius: 24rpx;
						border-bottom-left-radius: 24rpx;
					}
					._content-btn{
						width: 162rpx;
						height: 100%;
						border-top-right-radius: 24rpx;
						border-bottom-right-radius: 24rpx;
						background: linear-gradient(223deg, #A339D8 0%, #6948F2 100%);
						font-size: 32rpx;
						font-weight: 500;
						color: #FFFFFF;
						line-height: 96rpx;
						text-align: center;
					}
				}
				.myExchange-main-list{
					margin-top: 68rpx;
					.myExchange-main-list-item{
						width: 100%;
						display: flex;
						flex-wrap: nowrap;
						justify-content: space-between;
						align-items: center;
						margin-top: 42rpx;
						._list-item-label{
							font-size: 28rpx;
							font-weight: 400;
							color: #FFFFFF;
							line-height: 48rpx;
						}
						._list-item-btn{
							font-size: 28rpx;
							font-weight: 400;
							color: #9D7DFF;
							line-height: 48rpx;
						}
					}
				}
			}
		}
		.exchangePopup{
			width: 549rpx;
			height: 900rpx;
			background: transparent;
			font-family: PingFangSC-Medium, PingFang SC, '微软雅黑';
			font:caption;
			position: relative;
			box-sizing: border-box;
			.exchangeBox{
				width: 100%;
				height: 710rpx;
				background: #262A34;
				border-radius: 32px;
				.exchangeBox-goods{
					width: 100%;
					height: 549rpx;
					border-radius: 32px;
					overflow: hidden;
					.exchangeBox-goods-img{
						width: 100%;
						height: 100%;
					}
				}
				.exchangeBox-title{
					font-size: 28rpx;
					font-weight: 400;
					color: #FFFFFF;
					line-height: 56rpx;
					text-align: center;
					margin-top: 18rpx;
				}
				.exchangeBox-money{
					font-size: 36rpx;
					font-weight: 500;
					color: #FFFFFF;
					line-height: 40rpx;
					text-align: center;
					margin-top: 16rpx;
				}
			}
			.exchange-btn{
				width: 462rpx;
				height: 88rpx;
				margin: 102rpx auto 0;
				background: linear-gradient(223deg, #A339D8 0%, #6948F2 100%);
				border-radius: 50rpx;
				text-align: center;
				line-height: 88rpx;
				font-size: 32rpx;
				font-weight: 500;
				color: #FFFFFF;
			}
		}
		.tipsPopup{
			width: 590rpx;
			height: 610rpx;
			background: #262A34;
			border-radius: 40rpx;
			font-family: PingFangSC-Medium, PingFang SC, '微软雅黑';
			font:caption;
			position: relative;
			padding: 0 86rpx;
			box-sizing: border-box;
			.tipsPopup-icon{
				width: 168rpx;
				height: 168rpx;
				margin: 68rpx auto 24rpx;
				.tipsPopup-icon-img{
					width: 100%;
					height: 100%;
				}
			}
			.tipsPopup-title{
				font-size: 36rpx;
				font-weight: 500;
				color: #FFFFFF;
				text-align: center;
			}
			.tipsPopup-text{
				font-size: 32rpx;
				font-weight: 400;
				color: #FFFFFF;
				line-height: 56rpx;
				text-align: center;
				margin-top: 44rpx;
			}
			.tipsPopup-confirm{
				width: 406rpx;
				height: 88rpx;
				margin: 68rpx auto 0;
				background: linear-gradient(223deg, #A339D8 0%, #6948F2 100%);
				border-radius: 45rpx;
				font-size: 32rpx;
				font-weight: 500;
				color: #ffffff;
				line-height: 88rpx;
				text-align: center;
			}
			.cancel-icon{
				width: 40rpx;
				height: 40rpx;
				position: absolute;
				top: 40rpx;
				right: 48rpx;
			}
		}
	}
</style>
